#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "link_puc.h"

void show_datas_stu(Students *stus){
    if(stus == NULL){
        printf("%c没有学生信息\n", eg);
        return;
    }
    printf("%c%s学生信息%s\n", eg, ty, ty);
    printf("%c学号\t姓名\t已选课程 已获取学分\n", eg);
    while(stus != NULL){
        Student s = stus->stu;
        printf("%c%3d\t%-3s\t   %2d\t   %2.1f\t\t\n", eg, s.id, s.name, s.num_sub, s.sum_credit);
        stus = stus->next;
    }
    free_stus(stus);
}

void add_data_stu(){
    printf("%c%s添加学生信息%s\n", eg, ty, ty);
    int id;
    char *name = (char*) malloc(10*sizeof(char));
    printf("%c输入学号：", eg);
    scanf("%d", &id);
    printf("%c输入姓名：", eg);
    scanf("%s", name);
    Student stu;
    stu.id = id;
    stu.name = name;
    stu.num_sub = 0;
    stu.sum_credit = 0;
    stu.subs = NULL;
    BackCall call = put_stu(stu);
    if(call.code == 1){
        printf("%c增加成功\n", eg);
    }else{
        printf("%c%s\n", eg, call.msg);
    }
    free_stu(stu);
}

void delete_data_stu(){
    printf("%c%s删除信息%s\n", eg, ty, ty);
    printf("%c输入需要删除学生的学号：", eg);
    int id;
    scanf("%d", &id);
    BackCall call = delete_stu_id(id);
    if (call.code == 1){
        printf("%c删除成功\n", eg);
    }else{
        printf("%c%s\n", eg, call.msg);
    }
}

void update_date_stu(){
    printf("%c%s修改数据%s\n", eg, ty, ty);
    printf("%c输入需要修改学生的学号：", eg);
    int id;
    scanf("%d", &id);
    getchar();
    BackCall call = get_stu_id(id);
    if (call.code != 1 || call.stus == NULL){
        printf("%c找不到该学号的信息\n", eg);
        return;
    }
    Student s1 = call.stus->stu;

    char *name = (char*) malloc (10*sizeof(char));
    printf("%c学号(%d)\n", eg, id);
    printf("%c输入姓名(%s):", eg, s1.name);
    int i = 0;
    while((name[i++] = getchar()) != '\n'){}
    name[i-1] = '\0';
    if(name[0] == '\n'){
        strcpy(name, s1.name);
    }
    Student s2;
    s2.id = id;
    s2.name = name;
    s2.num_sub = s1.num_sub;
    s2.subs = s1.subs;
    s2.sum_credit = s1.sum_credit;
    call = update_stu(s2);
    if(call.code == 1){
        printf("%c更改成功\n", eg);
    }else{
        printf("%c%s\n", eg, call.msg);
    }
    free_stu(s1);
    free_stu(s2);
}

void show_data_stu(){
    printf("%c%s学生详细信息%s\n", eg, ty, ty);
    printf("%c输入学生学号或姓名：", eg);
    int id;
    char name[20];
    scanf("%s", name);
    BackCall call;
    if((id = atoi(name)) == 0){
        call = get_stu_name(name);
    }else{
        call = get_stu_id(id);
    }
    if(call.code != 1){
        printf("%c没有找到该学生信息\n", eg);
        return;
    }
    printf("%c学号\t姓名\t已选课程 已获取学分\n", eg);
    Student stu = call.stus->stu;
    printf("%c%3d\t%-3s\t   %2d\t   %2.1f\t\n", \
           eg, stu.id, stu.name, stu.num_sub, stu.sum_credit);
    call = get_sub_by_stu(stu);
    if(call.code != 1){
        printf("%c该学生还未选择课程\n", eg);
        return;
    }
    printf("已选课程如下：\n");
    printf("%c%s\t%-15s\t%s\t%s\t%s\t%s\n", \
                   eg, "编号", "课程名", "性质", \
                   "课时", "学分", "学生人数");
    Subjects *subs = call.subs;
    while(subs != NULL){
        Subject sub = subs->sub;
        printf("%c%3d\t%-15s\t%-3s\t%3d\t%.1f\t%2d/%-2d\t\n", \
                 eg, sub.id, sub.name, sub.nature, \
                 sub.period, sub.credit, sub.num, sub.max_num);
        subs = subs->next;
    }
    free_subs(subs);
    free_stu(stu);
}

void show_init_stu(){
    printf("%c添加学生信息(1)\n", eg);
    printf("%c删除学生信息(2)\n", eg);
    printf("%c修改学生信息(3)\n", eg);
    printf("%c查看学生详细信息(4)\n", eg);
    printf("%c查看所有学生信息(5)\n", eg);
    printf("%c退出(0)：\n", eg);
    printf("%c请选择操作(0-5)：", eg);
}

int show_stu(Students *stus){
    printf("%c%s学生管理界面%s\n", eg, ty, ty);
    if(stus != NULL){
        show_datas_stu(stus);
    }
    int type;
    do{
        show_init_stu();
        scanf("%d", &type);
        getchar();
        if(type == 0){
            break;
        }
        switch(type){
        case 1: add_data_stu();
            break;
        case 2: delete_data_stu();
            break;
        case 3: update_date_stu();
            break;
        case 4: show_data_stu();
            break;
        case 5: show_datas_stu(get_stu_all().stus);
            break;
        default:
                printf("%c输入出错\n", eg);
        }

    }while(1);
    return 0;
}
